#include "PKMvpWidget.h"

CPKMvpWidget::CPKMvpWidget(QWidget *parent) : QWidget(parent)
{
    m_bIsPressed = false;
    setMouseTracking(true);
    setCursor(Qt::PointingHandCursor);
    setStyleSheet("background-color:green;");

    QPoint pt(0,0);
    setGeometry(parent->width()/2-m_mvpIconSize.width()/2,0,parent->width(),parent->height());

    m_pLabelHead = new QLabel(this);
    m_pLabelHead->setGeometry(pt.x()+m_mvpIconSize.width()/2-m_mvpHeadSize.width()/2,pt.y()+m_mvpIconSize.height()/2-m_mvpHeadSize.height()/2,m_mvpHeadSize.width(),m_mvpHeadSize.height());


    m_pLabelBorder = new QLabel(this);
    m_pLabelBorder->setGeometry(pt.x(),pt.y(),m_mvpIconSize.width(),m_mvpIconSize.height());
}

void CPKMvpWidget::setIconSize(int width,int height)
{
    if(m_mvpIconSize.width()!= width ||m_mvpIconSize.height()!=height)
    {
        m_mvpIconSize = QSize(width,height);
    }
}

void CPKMvpWidget::setHeadIconSize(int width,int height)
{
    if(m_mvpHeadSize.width()!= width ||m_mvpHeadSize.height()!=height)
    {
        m_mvpHeadSize = QSize(width,height);
    }
}

void CPKMvpWidget::setHeadBorderIcon(QString url)
{
    if(m_mvpBorderUrl != url)
    {
        m_mvpBorderUrl = url;
        updateUI();
    }
}

void CPKMvpWidget::setHeadIcon(QString url)
{
    if(url != m_mvpHeadIconUrl)
    {
        m_mvpHeadIconUrl = url;
        updateUI();
    }
}

void CPKMvpWidget::updateUI(bool resize)
{
    if(resize){
        QPoint pt(0,0);
        m_pLabelHead->setGeometry(pt.x()+m_mvpIconSize.width()/2-m_mvpHeadSize.width()/2,pt.y()+m_mvpIconSize.height()/2-m_mvpHeadSize.height()/2,m_mvpHeadSize.width(),m_mvpHeadSize.height());
        m_pLabelBorder->setGeometry(pt.x(),pt.y(),m_mvpIconSize.width(),m_mvpIconSize.height());
    }

    bool ret = false;
    QPixmap pixmapHead;
    ret = pixmapHead.load(m_mvpHeadIconUrl);
    if(ret){
        QPixmap fitpixmapHead = pixmapHead.scaled(m_pLabelHead->width(), m_pLabelHead->height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); // 按比例缩放
        m_pLabelHead->setPixmap(fitpixmapHead);
    }

    bool ret2 = false;
    QPixmap pixmap;
    ret2 = pixmap.load(m_mvpBorderUrl);
    if(ret2){
        QPixmap fitpixmap = pixmap.scaled(m_pLabelBorder->width(), m_pLabelBorder->height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); // 按比例缩放
        m_pLabelBorder->setPixmap(fitpixmap);
    }

}


void CPKMvpWidget::mousePressEvent(QMouseEvent *ev)
{
    QWidget::mousePressEvent(ev);
    m_bIsPressed = true;
}

void CPKMvpWidget::mouseReleaseEvent(QMouseEvent *ev)
{
    QWidget::mouseReleaseEvent(ev);
    if (m_bIsPressed)
    {
        emit clicked();

        m_bIsPressed = false;
    }
}
